package edu.yctc.origin.controller.rest.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.origin.constant.ResultCode;
import edu.yctc.origin.controller.rest.MetadataRestController;
import edu.yctc.origin.entity.ResultDO;
import edu.yctc.origin.service.MetadataIService;
import edu.yctc.origin.vo.BookAndUrlVO;
import edu.yctc.origin.vo.BookVO;

/**
 * 元数据管理rest实现
 * 
 * @author 赵宜珺
 *
 */
@RestController
@ComponentScan({"edu.yctc.origin.service"})
@RequestMapping("/restmetadata")
public class MetadataRestControllerImpl implements MetadataRestController {

    private static final String BOOK_DETAIL = "/metadata/book-detail?id=";

    private static final String BOOK_MODIFY = "/metadata/book-modify?id=";

    @Autowired
    private MetadataIService metadataService;

    @Override
    @PostMapping("get-bookandurlvos-by-name")
    public ResultDO<List<BookAndUrlVO>> getBookAndUrlVOsByName(@RequestBody String name) {
        if (StringUtils.isBlank(name)) {
            return new ResultDO<List<BookAndUrlVO>>(false, ResultCode.PARAMETER_INVALID,
                ResultCode.MSG_PARAMETER_INVALID, null);
        }
        ResultDO<List<BookVO>> resultDO = metadataService.getBookVOsByName(name);
        if (resultDO.isSuccess() == false) {
            return new ResultDO<List<BookAndUrlVO>>(false, resultDO.getCode(), resultDO.getMsg(), null);
        }
        // 构建VO
        List<BookVO> bookVOs = resultDO.getModule();
        List<BookAndUrlVO> bookAndUrlVOs = new ArrayList<>();
        for (BookVO bookVO : bookVOs) {
            BookAndUrlVO bookAndUrlVO = new BookAndUrlVO();
            bookAndUrlVO.setBookVO(bookVO);
            bookAndUrlVO.setUrl(BOOK_DETAIL + bookVO.getId());
            bookAndUrlVOs.add(bookAndUrlVO);
        }
        return new ResultDO<List<BookAndUrlVO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, bookAndUrlVOs);
    }

    @Override
    @PostMapping("get-bookvo-by-bookid")
    public ResultDO<BookVO> getBookVOByBookId(@RequestBody long bookId) {
        if (bookId <= 0) {
            return new ResultDO<BookVO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        return metadataService.getBookVOByBookId(bookId);
    }

    @Override
    @PostMapping("delete-book-by-id")
    public ResultDO<Void> deleteBookById(@RequestBody long id) {
        if (id <= 0) {
            return new ResultDO<Void>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return metadataService.deleteBookById(id);
    }

    @Override
    @PostMapping("get-book-modify-url")
    public ResultDO<String> getBookModifyUrl(@RequestBody long id) {
        if (id <= 0) {
            return new ResultDO<String>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        String url = BOOK_MODIFY + id;
        return new ResultDO<String>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, url);
    }

    @Override
    @PostMapping("get-book-detail-url")
    public ResultDO<String> getBookDetailUrl(@RequestBody long id) {
        if (id <= 0) {
            return new ResultDO<String>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        String url = BOOK_DETAIL + id;
        return new ResultDO<String>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, url);
    }

    @Override
    @PostMapping("modify-book")
    public ResultDO<Void> modifyBook(@RequestBody BookVO bookVO) {
        if (bookVO.getId() <= 0 || bookVO.getStock() < 0 || StringUtils.isBlank(bookVO.getAuthor())
            || StringUtils.isBlank(bookVO.getName()) || StringUtils.isBlank(bookVO.getNumber())
            || StringUtils.isBlank(bookVO.getPress())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return metadataService.modifyBook(bookVO);
    }
}
